﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Models.DataGrid;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels
{
    public class ROADViewModel:ViewModelBase
    {
        /// <summary>
        /// ctor
        /// </summary>
        public ROADViewModel() 
        {
            data = ServiceLocator.Current.GetInstance<RCSDDataModel>().Data;
            OADList=new ObservableCollection<SingleData> { };
            int oadNum = 0;
            int point = 10;
            if (data?.Length>=10)
            {
                OAD = data.Substring(0, 8);
                oadNum = int.Parse(data.Substring(8,2));
            }
            for (int i = 0; i < oadNum; i++)
            {
                SingleData singleData = new SingleData();
                if (point+8> data.Length)
                {
                    break;
                }
                singleData.Name = data.Substring(point, 8);
                OADList.Add(singleData);
                point += 8;
            }

        }

        private string data;

        private string oad;

        public string OAD
        {
            get { return oad; }
            set { Set(ref oad ,value); }
        }

        private ObservableCollection<SingleData> oadList;

        public ObservableCollection<SingleData> OADList
        {
            get { return oadList; }
            set { Set(ref oadList,value); }
        }

        public RelayCommand<Window> OKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    data = OAD;
                    data += OADList.Count.ToString("X2");
                    for (int i = 0; i < OADList.Count; i++)
                    {
                        data += OADList[i].Name;
                    }
                    ServiceLocator.Current.GetInstance<RCSDDataModel>().Data = data;
                    w.DialogResult = true;
                });
                return command;
            }
        }

        public RelayCommand<Window> CancelCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    
                    w.DialogResult = false;
                });
                return command;
            }
        }
    }
}
